package com.xuecheng.content.controller;

import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.CoursePubVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.CoursePub;
import com.xuecheng.content.service.CoursePubService;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.rmi.MarshalledObject;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;
    @Autowired
    private MinioTemplate minioTemplate;
    @Autowired
    private CoursePubService coursePubService;
    //分页+条件查询
    @PostMapping("/course/list")
    public ResponseResult<CourseBaseVo> findByPage(PageRequest pageRequest,
                                                   @RequestBody CourseDto dto){
        //调用service
       return courseBaseService.findByPage(pageRequest,dto);

    }
    //图片上传
    @PostMapping("/course/upload")
    public ResponseResult<String> addPicture(MultipartFile uploadFile) throws IOException {
        //调用Minio配置类来进行图片上传
        String upload = minioTemplate.upload(uploadFile.getInputStream(), uploadFile.getOriginalFilename());
        System.out.println(upload);
        //返回文件地址
        return ResponseResult.okResult(upload);

    }
    //课程新增
    @PostMapping("/course")
    public ResponseResult<Map> addCourse(@RequestBody CourseBaseDto dto){
        //调用service进行新增
        ResponseResult<Map> map= courseBaseService.insert(dto);
       //返回结果
       return map;

    }
    //课程回显
    @GetMapping("/course/{courseBaseId}")
    public ResponseResult<CourseBaseVo> findById(@PathVariable("courseBaseId")Long id){
        //调用service
        CourseBase courseBase = courseBaseService.getById(id);
        //转Vo
        CourseBaseVo courseBaseVo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        //返回结果
        return ResponseResult.okResult(courseBaseVo);
    }
    //课程修改
    @PutMapping("/course")
    public ResponseResult<Map> update(@RequestBody CourseBaseDto dto){//接收参数
        //调用service
       return courseBaseService.updateCourse(dto);

    }
    // 课程删除
    @DeleteMapping("/course/{courseId}")
    public ResponseResult deleteCourseBase(@PathVariable("courseId") Long courseId){
        return courseBaseService.deleteCourseBase(courseId);

    }
    // 提交审核
    @GetMapping("/course/commit/{courseBaseId}")
    public ResponseResult commitCourseBase(@PathVariable("courseBaseId") Long courseBaseId){
        // update tb_course_base set audit_status = xxx where id = xx
        courseBaseService.lambdaUpdate()
                .set(CourseBase::getAuditStatus, CourseConstants.AUDIT_COMMIT_STATUS)
                .eq(CourseBase::getId,courseBaseId).update();
        return ResponseResult.okResult();
    }
    // 课程审核
    @PostMapping("/course/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto) {

        // update tb_course_base set audit_status = xxx and audit_mind = xxx where id = xx
        courseBaseService.lambdaUpdate()
                .set(CourseBase::getAuditStatus, dto.getAuditStatus())
                .set(CourseBase::getAuditMind, dto.getAuditMind())
                .eq(CourseBase::getId, dto.getCourseBaseId()).update();

        return ResponseResult.okResult();
    }

}
